#!/bin/bash

set -eux

POOL_NAME={{ libvirt_volume_pool }}
POOL_PATH={{ libvirt_volume_path }}
CLONEFROM={{ image_cache_dir }}/supplemental_base.qcow2
VMSSHKEY={{ working_dir }}/id_rsa_supplemental.pub
VMDISKADD=30G

IMG_NAME=ipa.qcow2
VMIMGIPA={{ working_dir }}/$IMG_NAME
VMIMGIPACOPY={{ working_dir }}/ORIG-$IMG_NAME
VOL_IMG_PATH=$POOL_PATH/$IMG_NAME

# Copy qcow2 base image
cp -v $CLONEFROM $VMIMGIPA

# Resize the FreeIPA image
echo "$(date) - Adding $VMDISKADD to $VMIMGIPA: "
qemu-img resize $VMIMGIPA +$VMDISKADD

echo "$(date) - Resizing filesystem of $VMIMGIPA: "
cp -v $VMIMGIPA $VMIMGIPACOPY
virt-resize --expand /dev/sda1 $VMIMGIPACOPY $VMIMGIPA
rm -fv $VMIMGIPACOPY

echo "$(date) - Checking status of $VMIMGIPA: "
qemu-img info $VMIMGIPA
virt-filesystems --long -h --all -a $VMIMGIPA

cat > ifcfg-eth0 <<EOF
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR={{ freeipa_internal_ip }}
NETMASK=255.255.255.0
GATEWAY={{ supplemental_tls_everywhere_gateway }}
PEERDNS=yes
DNS1={{ supplemental_tls_everywhere_dns_server }}
TYPE=Ethernet
EOF

cat > ifcfg-eth1 <<EOF
NAME=eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
EOF

# NOTE(hrybacki): The ugly is formed as such because Ansible <2.3 lacks the ability to control
#                 whitespace trimming in jinja templates. This results in the inability to form
#                 a proper newline. http://docs.ansible.com/ansible/template_module.html#options
virt-customize -a $VMIMGIPA \
    --root-password password:redhat \
    --install openssh-server \
    --run-command "xfs_growfs /" \
    --run-command "echo 'GRUB_CMDLINE_LINUX=\"console=tty0 crashkernel=auto no_timer_check net.ifnames=0 console=ttyS0,115200n8\"' >> /etc/default/grub" \
    --run-command "grubby --update-kernel=ALL --args=\"net.ifnames=0 cloud-init=disabled\"" \
    --run-command "systemctl enable sshd" \
    {{ "--run-command \"useradd " + supplemental_user + " -m -p ''\" \\" if supplemental_user != 'root' else "\\" }}
    {{ "--run-command \"echo " + supplemental_user + " ALL=\\(ALL\\) NOPASSWD:ALL >> /etc/sudoers\" \\" if supplemental_user != 'root' else "\\" }}
    --mkdir /root/.ssh \
    --copy-in ifcfg-eth0:/etc/sysconfig/network-scripts/ \
    --copy-in ifcfg-eth1:/etc/sysconfig/network-scripts/ \
    --ssh-inject {{ supplemental_user }}:file:$VMSSHKEY \
    --selinux-relabel

# We want the supplemental node to be a member of the correct volume
# pool. If we provide virt-install with just a path to the image
# then virt-install will create a pool and volumne for the image
# and we will have lost knowledge of where virsh is maintaining
# the image. So create a volume in the desired pool of the correct
# size and then upload our image into that pool/volume. Then tell
# virt-install what the path is to the volume. virt-install will be
# able to determine the volume and pool from the path.

VOL_SIZE=`qemu-img info --output=json $VMIMGIPA | \
          python -c "import sys,json; \
          info=json.load(sys.stdin); print(info['virtual-size'])"`

virsh vol-create-as $POOL_NAME $IMG_NAME $VOL_SIZE --format qcow2
virsh vol-upload --pool $POOL_NAME $IMG_NAME $VMIMGIPA

# Deploy the ipa vm
virt-install \
    --import \
    --name ipa \
    --ram 10240 \
    --disk path=$VOL_IMG_PATH,format=qcow2 \
    --vcpus 4 \
    --os-type linux \
    --os-variant generic \
    --network bridge=brext \
    --graphics none \
    --noautoconsole
